package com.coldwindblows.blog.controller.admin;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.coldwindblows.blog.bo.RestResponseBo;
import com.coldwindblows.blog.constant.WebConst;
import com.coldwindblows.blog.controller.BaseController;
import com.coldwindblows.blog.dto.LogActions;
import com.coldwindblows.blog.dto.Types;
import com.coldwindblows.blog.pojo.AttachDo;
import com.coldwindblows.blog.pojo.UserDo;
import com.coldwindblows.blog.service.IAttachService;
import com.coldwindblows.blog.service.ILogService;
import com.coldwindblows.blog.utils.Commons;
import com.coldwindblows.blog.utils.TaleUtils;
import com.github.pagehelper.PageInfo;

/**
 * 附件管理
 * Created by ColdWindBlows on 2018/07/06
 *
 */
@Controller
@RequestMapping("admin/attach")
@SuppressWarnings("rawtypes")
public class AttachController extends BaseController {
	
	public static final String CLASSPATH = TaleUtils.getUploadFilePath();
	
	@Autowired
	private static IAttachService attachService;
	
	@Autowired
	private ILogService logService;
	
	/**
	 * 附件页面
	 * @return
	 */
	@GetMapping(path = "")
	public String index(Model model, @RequestParam(value = "page", defaultValue = "1")int page, @RequestParam(value = "limit", defaultValue = "12")int limit) {
		PageInfo<AttachDo> pageList = attachService.getAttachs(page, limit);
		model.addAttribute("attachs", pageList);
		model.addAttribute(Types.ATTACH_URL.getType(), Commons.site_option(Types.ATTACH_URL.getType(), Commons.site_url()));
		model.addAttribute("max_file_size", WebConst.MAX_FILE_SIZE / 1024);
		
		return "admin/attach";
	}
	
	/**
	 * 上传文件接口
	 * @param request
	 * @param multipartFiles
	 * @return
	 */
	@PostMapping("/upload")
	@ResponseBody
	public RestResponseBo upload(HttpServletRequest request, @RequestParam("file")MultipartFile[] multipartFiles) {
		UserDo user = this.user(request);
		Integer uid = user.getUid();
		List<String> errorFiles = new ArrayList<String>();
		
		try {
			String fname = null;
			String fkey = null;
			String ftype = null;
			File file = null;
			for(MultipartFile me : multipartFiles) {
				fname = me.getOriginalFilename();
				if(me.getSize() > WebConst.MAX_FILE_SIZE) {
					errorFiles.add(fname);
					continue;
				}
				fkey = TaleUtils.getFileKey(fname);
				ftype = TaleUtils.isImage(me.getInputStream()) ? Types.IMAGE.getType() : Types.FILE.getType();
				file = new File(CLASSPATH + fkey);
				try {
					FileCopyUtils.copy(me.getInputStream(), new FileOutputStream(file));
				} catch(IOException e) {
					e.printStackTrace();
				}
				attachService.insertAttach(fname, fkey, ftype, uid);
			}
		} catch(Exception e) {
			return RestResponseBo.fail();
		}
		
		return RestResponseBo.ok();
	}
	
	@PostMapping(path = "/delete")
	@ResponseBody
	public RestResponseBo delete(Integer id, HttpServletRequest request) {
		AttachDo attach = attachService.getDetail(id);
		if(null == attach) {
			return RestResponseBo.fail("不存在该附件");
		}
		try {
			attachService.deleteAttach(id);
			new File(CLASSPATH + attach.getFkey()).delete();
			logService.insertLog(LogActions.DEL_ARTICLE.getAction(), attach.getFkey(), request.getRemoteAddr(), this.getUid(request));
		} catch(Exception e) {
			String msg = "附件删除失败";
			return RestResponseBo.fail(msg);
		}
		
		return RestResponseBo.ok();
	}
}
